package com.starbucks.opencase.tool;

import com.starbucks.opencase.entity.entProperties;
import com.starbucks.opencase.request.reqAdminCommand;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class toolProperties {
    @Autowired
    private JdbcTemplate con;
    public entProperties queryInfo(){
        try {
            List<entProperties> as = con.query("select * from properties order by timestamp desc limit 1", new BeanPropertyRowMapper<>(entProperties.class));
            if (as.isEmpty()) return null;
            return as.get(0);
        }catch (Exception e){
            return null;
        }
    }
    public boolean updatePlayer(String nickname,int energy,String pid){
        entProperties properties=queryInfo();
        properties.setTimestamp(System.currentTimeMillis());
        properties.setNuNickname(nickname);
        properties.setNuEnergy(energy);
        properties.setNuPid(pid);
        return insert(properties);
    }
    public boolean updateScan(int expendEnergy,float proEnergy,float proGoods,float proPet,float proBusinessman,float proPlayer,int proEnergyAward){
        entProperties properties=queryInfo();
        properties.setTimestamp(System.currentTimeMillis());
        properties.setExpendEnergy(expendEnergy);
        properties.setProEnergy(proEnergy);
        properties.setProGoods(proGoods);
        properties.setProPet(proPet);
        properties.setProBusinessman(proBusinessman);
        properties.setProPlayer(proPlayer);
        properties.setProEnergyAward(proEnergyAward);
        return insert(properties);
    }
    public boolean insert(entProperties properties){
        return con.update("insert into properties values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)",
                properties.getTimestamp(),properties.getNuNickname(),properties.getNuEnergy(),properties.getNuPid(),
                properties.getExpendEnergy(),properties.getProEnergy(),properties.getProGoods(),properties.getProPet(),
                properties.getProBusinessman(),properties.getProPlayer(),properties.getProEnergyAward(),
                properties.getRobotRewardEnergy(),properties.getWalletMaxEnergy(),properties.getMessageCacheDay())>0;
    }
}
